package com.myapp.ruyixing.ui.shopmanager;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.ImpressionList;
import com.myapp.ruyixing.model.ImpressionList.Impression;
import com.myapp.ruyixing.model.Model;
import com.myapp.ruyixing.ui.BaseActivity;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.BitmapLruCache;
import com.myapp.ruyixing.utils.LoginHelper;

// 印象管理
public class ImpressionManageActivity extends BaseActivity implements
		OnClickListener, OnItemClickListener {
	private int mCurrentTab = 0;

	private int mCurrentIndex = 0;

	private List<Impression> mList;
	private MyListAdapter mAdapter;
	private ImpressionList mDataList;

	private TitleBar mTitleBar;
	private View mLoadingView;
	private ListView mListView;
	private TextView mEmptyView;
	private View mFocusedTabView;
	private EditText mSearchView;

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				mDataList = (ImpressionList) o;
				onLoadingDataFinished(true, mDataList);
			} catch (Exception e) {
				onLoadingDataFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onLoadingDataFinished(false, null);
		}
	};
	private RYHttpResponseHandler mStatusHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				Model result = (Model) o;
				onChangeStatusFinished(true, result);
			} catch (Exception e) {
				onChangeStatusFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onChangeStatusFinished(false, null);
		}
	};

	private void onChangeStatusStarted(String id, String status) {
		showLoading();
		RYHttpClient.updateImpression(this, mStatusHttpResponseHandler,
				LoginHelper.getInstance().getShopBduss(), LoginHelper
						.getInstance().getShopId(), id, status);
	}

	private void onChangeStatusFinished(boolean succeed, Model result) {
		hideLoading();
		if (succeed && result != null && result.errno == 0) {
			Toast.makeText(this, "更新成功", Toast.LENGTH_SHORT).show();
			mList.remove(mCurrentIndex);
			mAdapter.notifyDataSetChanged();
		} else {
			Toast.makeText(this, "加载失败:" + (result == null ? "" : result.msg),
					Toast.LENGTH_SHORT).show();
		}
	}

	private void onLoadingDataStarted(String shopbduss, String shopid,
			int status, int pn, int rn) {
		showLoading();
		RYHttpClient.getImpressionList(this, mHttpResponseHandler, shopbduss,
				shopid, status + "", pn, rn);
	}

	private void onLoadingDataFinished(boolean succeed, ImpressionList actList) {
		hideLoading();
		if (succeed && actList != null && actList.items != null) {
			mList.clear();
			mEmptyView.setVisibility(View.GONE);
			mList.addAll(actList.items);
			mAdapter.notifyDataSetChanged();
			if (mList.size() == 0) {
				mEmptyView.setVisibility(View.VISIBLE);
			}
		} else {
			Toast.makeText(this,
					"加载失败:" + (actList == null ? "" : actList.msg),
					Toast.LENGTH_SHORT).show();
			mEmptyView.setVisibility(View.VISIBLE);
		}
	}

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_shop_manager_impression_manage);

		initView();

		mList = new ArrayList<Impression>();
		mAdapter = new MyListAdapter();
		mListView.setAdapter(mAdapter);

		onLoadingDataStarted(LoginHelper.getInstance().getShopBduss(),
				LoginHelper.getInstance().getShopId(), 0, 0, 50);
	}

	private void initView() {
		mTitleBar = (TitleBar) findViewById(R.id.title_bar);
		mLoadingView = findViewById(R.id.my_loading);
		mListView = (ListView) findViewById(android.R.id.list);
		mEmptyView = (TextView) findViewById(android.R.id.empty);
		mListView.setOnItemClickListener(this);

		initTab();

		mTitleBar.setOnIconClickedListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});

		mSearchView = (EditText) findViewById(R.id.edit_search);
		mSearchView.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				String s = mSearchView.getText().toString();
				s = s.trim();
				ImpressionList al = new ImpressionList();
				al.items = new ArrayList<ImpressionList.Impression>();
				if (mDataList != null && mDataList.items != null) {
					for (Impression act : mDataList.items) {
						if (act.username != null && act.username.contains(s)) {
							al.items.add(act);
						}
					}
				}
				onLoadingDataFinished(true, al);

				return false;
			}
		});
	}

	private void initTab() {
		mFocusedTabView = findViewById(R.id.about_to_check);
		mFocusedTabView.setOnClickListener(this);
		mFocusedTabView.setSelected(true);

		findViewById(R.id.check_pass).setOnClickListener(this);
		findViewById(R.id.check_not_pass).setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.about_to_check:
			mCurrentTab = 0;
			if (changeTab(v)) {
				mList.clear();
				mAdapter.notifyDataSetChanged();
				mSearchView.setText("");
				onLoadingDataStarted(LoginHelper.getInstance().getShopBduss(),
						LoginHelper.getInstance().getShopId(), 0, 0, 50);
			}
			break;
		case R.id.check_pass:
			mCurrentTab = 1;
			if (changeTab(v)) {
				mList.clear();
				mAdapter.notifyDataSetChanged();
				mSearchView.setText("");
				onLoadingDataStarted(LoginHelper.getInstance().getShopBduss(),
						LoginHelper.getInstance().getShopId(), 1, 0, 50);
			}

			break;
		case R.id.check_not_pass:
			mCurrentTab = 2;
			if (changeTab(v)) {
				mList.clear();
				mAdapter.notifyDataSetChanged();
				mSearchView.setText("");
				onLoadingDataStarted(LoginHelper.getInstance().getShopBduss(),
						LoginHelper.getInstance().getShopId(), 2, 0, 50);
			}

			break;
		case R.id.pass:
			mCurrentIndex = (Integer) v.getTag();
			onChangeStatusStarted(mList.get(mCurrentIndex).id, 1 + "");
			break;
		case R.id.reject:
			mCurrentIndex = (Integer) v.getTag();
			onChangeStatusStarted(mList.get(mCurrentIndex).id, 2 + "");
			break;

		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub

	}

	private boolean changeTab(View v) {
		if (mFocusedTabView == v) {
			return false;
		}

		mFocusedTabView.setSelected(false);
		mFocusedTabView = v;
		mFocusedTabView.setSelected(true);
		return true;
	}

	private void showTitleBarLoading() {
		mTitleBar.showLoading(true);
	}

	private void hideTitleBarLoading() {
		mTitleBar.showLoading(false);
	}

	private void showLoading() {
		mLoadingView.setVisibility(View.VISIBLE);
	}

	private void hideLoading() {
		mLoadingView.setVisibility(View.GONE);
	}

	private class ViewHolder {
		private TextView mNameView;
		private TextView mTimeView;
		private TextView mContentView;
		private View mPassView;
		private View mRejectView;
	}

	private class MyListAdapter extends BaseAdapter {

		private RequestQueue mQueue;
		private ImageLoader mImageLoader;

		public MyListAdapter() {
			super();
			mQueue = Volley.newRequestQueue(ImpressionManageActivity.this);
			mImageLoader = new ImageLoader(mQueue, new BitmapLruCache());
		}

		@Override
		public int getCount() {
			return mList == null ? 0 : mList.size();
		}

		@Override
		public Object getItem(int position) {
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater
						.from(ImpressionManageActivity.this).inflate(
								R.layout.cell_shop_manager_impression_manage,
								parent, false);

				ViewHolder holder = new ViewHolder();
				holder.mNameView = (TextView) convertView
						.findViewById(R.id.name);
				holder.mTimeView = (TextView) convertView
						.findViewById(R.id.time);
				holder.mContentView = (TextView) convertView
						.findViewById(R.id.content);
				holder.mPassView = convertView.findViewById(R.id.pass);
				holder.mRejectView = convertView.findViewById(R.id.reject);

				holder.mPassView
						.setOnClickListener(ImpressionManageActivity.this);
				holder.mRejectView
						.setOnClickListener(ImpressionManageActivity.this);

				convertView.setTag(holder);
			}

			ViewHolder mHolder = (ViewHolder) convertView.getTag();

			Impression impression = mList.get(position);

			if (mCurrentTab == 0) {
				mHolder.mPassView.setVisibility(View.VISIBLE);
				mHolder.mRejectView.setVisibility(View.VISIBLE);
			} else if (mCurrentTab == 1) {
				mHolder.mPassView.setVisibility(View.INVISIBLE);
				mHolder.mRejectView.setVisibility(View.VISIBLE);
			} else if (mCurrentTab == 2) {
				mHolder.mPassView.setVisibility(View.VISIBLE);
				mHolder.mRejectView.setVisibility(View.INVISIBLE);
			}

			mHolder.mContentView.setText(impression.content);
			mHolder.mNameView.setText(impression.username);
			mHolder.mTimeView.setText(impression.datetime);
			mHolder.mPassView.setTag(position);
			mHolder.mRejectView.setTag(position);

			return convertView;

		}
	}
}
